import {createRouter, createWebHashHistory,createWebHistory} from "vue-router";
import Films from "../views/Films.vue";
// import Cinemas from "../views/Cinemas.vue";
// import Center from "../views/Center.vue"
import NotFound from "../views/NotFound.vue";
import ComingSoon from "../views/films/ComingSoon.vue";
import NowPlaying from "../views/films/NowPlaying.vue";
import Detail from "../views/Detail.vue";
import Login from "../views/Login.vue";
import City from "../views/City.vue";

const routes = [
    {
        path: '/login',
        name: "login",
        component: Login
    },
    {
        path: '/films',
        name: "myfilms", //命名路由
        component: Films,
        children: [
            {
                path:"/films/nowplaying",
                component:NowPlaying
            },{
                path:"/films/comingsoon",
                component:ComingSoon
            },{
                path:"/films",
                redirect:"/films/nowplaying"
            }
        ]
    },
    {
        path: '/cinemas',
        component: ()=>import("../views/Cinemas.vue")
    },
    {
        path: '/center',
        component:  ()=>import("../views/Center.vue"),
        meta:{
            requiredAuth:true
        }
    },
    {
        path: '/',
        redirect:'/films'
    },
    {
        path: '/:pathMatch(.*)*',
        component: NotFound
    },{
        path:"/films/detail/:myId",
        component:Detail
    },{
        path:"/city",
        component:City
    }


]

const router = createRouter({
    // history: createWebHashHistory(),
    history: createWebHistory(),
    routes, // `routes: routes` 的缩写
})

//所有页面都拦截过滤
// router.beforeEach(async (to, from, next) => {
//     let  [isAuthenticated] = await Promise.all([localStorage.getItem("token")])
//     if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
//     else next()
// })

// router.beforeEach(async (to, from, next) => {
//     let  [isAuthenticated] = await Promise.all([localStorage.getItem("token")])
//     if (to.name !== 'login' && !isAuthenticated && to.meta.requiredAuth) next({ name: 'login' })
//     else next()
// })


export default router

